package com.hongshan.wallet.util

import android.databinding.BindingAdapter
import android.databinding.InverseBindingAdapter
import android.databinding.InverseBindingListener
import android.graphics.drawable.Drawable
import android.support.v4.view.ViewPager
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.DatePicker
import android.widget.ImageView
import android.widget.TextView
import android.widget.TimePicker
import com.aurelhubert.ahbottomnavigation.AHBottomNavigation
import com.bumptech.glide.Glide
import com.hongshan.wallet.R
import com.hongshan.wallet.base.BaseOnPageChangeListener
import com.hongshan.wallet.base.BaseViewTarget
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import java.util.*


/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.util
 * @author   : zhangxx
 * @date     : 2019/3/21
 * @email    : qq957413212@hotmail.com
 * @desc     : DataBinding适配器
 */
object BindingUtils {

    @JvmStatic
    @BindingAdapter("bind:image")
    fun image(imageView: ImageView, url: String?) {
        if (url != null && url.isNotEmpty()) Glide.with(imageView).load(url).into(imageView)
    }

    @JvmStatic
    @BindingAdapter("bind:image")
    fun image(imageView: ImageView, res: Int?) {
        if (res != null) {
            imageView.setImageResource(res)
        }
    }

    @JvmStatic
    @BindingAdapter("bind:enable")
    fun enable(view: View, isEnable: Boolean?) {
        if (isEnable != null) {
            view.isEnabled = isEnable
        }
    }

    // ViewPagerItemBind
    @JvmStatic
    @BindingAdapter("bind:currentItem")
    fun setItem(vp: ViewPager, index: Int) {
        vp.currentItem = index
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "bind:currentItem", event = "itemListener")
    fun getItem(vp: ViewPager) = vp.currentItem

    @JvmStatic
    @BindingAdapter("bind:itemListener")
    fun listenItem(vp: ViewPager, listener: InverseBindingListener) {
        vp.addOnPageChangeListener(BaseOnPageChangeListener { position ->
            listener.onChange()
        })
    }

    // AHBottomNavigationBind
    @JvmStatic
    @BindingAdapter("bind:currentItem")
    fun currentItem(navigation: AHBottomNavigation, index: Int) {
        navigation.currentItem = index
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "bind:currentItem", event = "itemListener")
    fun getItem(navigation: AHBottomNavigation) = navigation.currentItem

    @JvmStatic
    @BindingAdapter("bind:itemListener")
    fun listenItem(navigation: AHBottomNavigation, listener: InverseBindingListener) {
        navigation.setOnTabSelectedListener {_,_ ->
            listener.onChange()
            return@setOnTabSelectedListener true
        }
    }

    @JvmStatic
    @BindingAdapter("bind:isShow")
    fun isShow(view: View, isShow: Boolean) {
        view.clearAnimation()
        if (isShow) {
            view.animation = AnimationUtils.loadAnimation(view.context, R.anim.view_show)
            view.visibility = View.VISIBLE
            view.animation.start()
        } else {
            view.animation = AnimationUtils.loadAnimation(view.context, R.anim.view_hide)
            view.visibility = View.GONE
            view.animation.start()
        }

    }

    @JvmStatic
    @BindingAdapter("bind:isShowOnly")
    fun isShowOnly(view: View, isShow: Boolean) {
        view.visibility = if (isShow) {
            View.VISIBLE
        } else {
            View.GONE
        }
    }

    @JvmStatic
    @BindingAdapter("bind:onPagerChangeListener")
    fun onPagerChangeListener(vp: ViewPager, listener: ViewPager.OnPageChangeListener) {
        vp.setOnPageChangeListener(listener)
    }

    // DatePickerBind
    @JvmStatic
    @BindingAdapter("bind:date")
    fun updateDate(dp: DatePicker, time: Long) {
        val calendar = GregorianCalendar()
        calendar.timeInMillis = time * 1000
        dp.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "bind:date", event = "dateListener")
    fun getDate(dp: DatePicker): Long {
        val calendar = GregorianCalendar()
        calendar.set(dp.year, dp.month, dp.dayOfMonth, 0, 0, 0)
        return calendar.timeInMillis / 1000
    }

    @JvmStatic
    @BindingAdapter("bind:dateListener")
    fun listenDateChange(dp: DatePicker, listener: InverseBindingListener) {
        dp.init(
            dp.year, dp.month, dp.dayOfMonth
        ) { _, _, _, _ -> listener.onChange() }
    }

    // TimePickerBind
    @JvmStatic
    @BindingAdapter("bind:time")
    fun updateTime(tp: TimePicker, time: Long) {
        val calendar = GregorianCalendar()
        calendar.timeInMillis = time * 1000
        tp.currentHour = calendar.get(Calendar.HOUR_OF_DAY)
        tp.currentMinute = calendar.get(Calendar.MINUTE)
//        dp.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))
    }

    @JvmStatic
    @InverseBindingAdapter(attribute = "bind:time", event = "timeListener")
    fun getTime(dp: TimePicker): Long {
        val calendar = GregorianCalendar()
        calendar.set(Calendar.HOUR_OF_DAY,dp.currentHour)
        calendar.set(Calendar.MINUTE,dp.currentMinute)
        calendar.set(Calendar.SECOND,0)
//        calendar.set(dp.year, dp.month, dp.dayOfMonth)
        return calendar.timeInMillis / 1000
    }

    @JvmStatic
    @BindingAdapter("bind:timeListener")
    fun listenTimeChange(dp: TimePicker, listener: InverseBindingListener) {
//        dp.init(
//            dp.year, dp.month, dp.dayOfMonth
//        ) { _, _, _, _ -> listener.onChange() }
        dp.setOnTimeChangedListener { _, _, _ -> listener.onChange() }
    }

    @JvmStatic
    @BindingAdapter("bind:background")
    fun background(view: View, res:Int?) {
        if (res != null) Glide.with(view).load(res).into(BaseViewTarget(view))
    }

    @JvmStatic
    @BindingAdapter("bind:background")
    fun background(view: View, url:String?) {
        if (url != null && url.isNotEmpty()) Glide.with(view).load(url).into(BaseViewTarget(view))
    }

    @JvmStatic
    @BindingAdapter("bind:background")
    fun background(view: View, drawable:Drawable) {
        view.background = drawable
    }

    @JvmStatic
    @BindingAdapter("bind:textColor")
    fun textColor(tv:TextView, color:Int){
        tv.setTextColor(color)
    }

    @JvmStatic
    @BindingAdapter("bind:onRefresh")
    fun onRefresh(srl: SmartRefreshLayout, listener: OnRefreshListener){
        srl.setOnRefreshListener(listener)
    }

    @JvmStatic
    @BindingAdapter("bind:onLoad")
    fun onLoad(srl:SmartRefreshLayout, listener:OnLoadMoreListener){
        srl.setOnLoadMoreListener(listener)
    }

    @JvmStatic
    @BindingAdapter("bind:refreshEnable")
    fun refreshEnable(srl: SmartRefreshLayout, enable: Boolean){
        srl.setEnableRefresh(enable)
    }

    @JvmStatic
    @BindingAdapter("bind:loadMoreEnable")
    fun loadMoreEnable(srl:SmartRefreshLayout, enable:Boolean){
        srl.setEnableLoadMore(enable)
    }
}